package io.finer.erp.jeecg.sale.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.finer.erp.jeecg.approve.entity.ApproveRecord;
import io.finer.erp.jeecg.purchase.entity.PurchaseRequest;
import io.finer.erp.jeecg.purchase.entity.PurchaseRequestLine;
import io.finer.erp.jeecg.purchase.service.PurchaseRequestLineService;
import io.finer.erp.jeecg.purchase.service.PurchaseRequestService;
import io.finer.erp.jeecg.purchase.vo.PurchaseRequestPage;
import io.finer.erp.jeecg.sale.entity.SaleOrder;
import io.finer.erp.jeecg.sale.entity.SaleOrderLine;
import io.finer.erp.jeecg.sale.service.SaleOrderLineService;
import io.finer.erp.jeecg.sale.service.SaleOrderService;
import io.finer.erp.jeecg.sale.vo.SaleOrderPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 销售订单 前端控制器
 * </p>
 *
 * @author yaoyifan
 * @since 2021-05-11
 */
@Api(tags = "销售订单")
@RestController
@RequestMapping("/sale/saleOrder")
@Slf4j
public class SaleOrderController {
    @Autowired
    SaleOrderService saleOrderService;

    @Autowired
    SaleOrderLineService saleOrderLineService;

    @AutoLog(value="销售订单-分页列表查询")
    @ApiOperation(value="销售订单-分页列表查询", notes="销售订单-分页列表查询")
    @GetMapping("/list")
    public Result<?> queryPageList(SaleOrder saleOrder,
                                   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                   HttpServletRequest req){
        QueryWrapper<SaleOrder> queryWrapper = QueryGenerator.initQueryWrapper(saleOrder, req.getParameterMap());
        Page<SaleOrder> page = new Page<>(pageNo, pageSize);
        IPage<SaleOrder> pageList = saleOrderService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    @AutoLog(value = "销售订单-添加")
    @ApiOperation(value="销售订单-添加", notes="销售订单-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody SaleOrderPage saleOrderPage) {
        SaleOrder saleOrder = new SaleOrder();
        BeanUtils.copyProperties(saleOrderPage, saleOrder);
        saleOrderService.saveMain(saleOrder, saleOrderPage.getSaleOrderLineList());
        return Result.ok("添加成功！");
    }

    @AutoLog(value = "销售订单-编辑")
    @ApiOperation(value="销售订单-编辑", notes="销售订单-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody SaleOrderPage saleOrderPage) {
        SaleOrder saleOrder = new SaleOrder();
        BeanUtils.copyProperties(saleOrderPage, saleOrder);
        SaleOrder saleOrderEntity = saleOrderService.getById(saleOrder.getId());
        if(saleOrderEntity==null) {
            return Result.error("未找到对应数据");
        }
        saleOrderService.updateMain(saleOrder, saleOrderPage.getSaleOrderLineList());
        return Result.ok("编辑成功!");
    }

    @AutoLog(value = "销售订单-删除")
    @ApiOperation(value="销售订单-删除", notes="销售订单-删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name="id", required = true) String id){
        this.saleOrderService.delMain(id);
        return Result.ok("删除成功!");
    }


    @AutoLog(value = "销售订单-批量删除")
    @ApiOperation(value="销售订单-批量删除", notes="销售订单-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
        this.saleOrderService.delBatchMain(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功！");
    }

    @AutoLog(value = "销售订单-通过id查询")
    @ApiOperation(value="销售订单-通过id查询", notes="销售订单-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
        SaleOrder saleOrder = saleOrderService.getById(id);
        if(saleOrder==null) {
            return Result.error("未找到对应数据");
        }
        return Result.ok(saleOrder);
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "明细通过主表ID查询")
    @ApiOperation(value="明细主表ID查询", notes="明细-通主表ID查询")
    @GetMapping(value = "/queryLineByMainId")
    public Result<?> queryLineListByMainId(@RequestParam(name="id",required=true) String id) {
        QueryWrapper<SaleOrderLine> queryWrapper = new QueryWrapper<>();
        List<SaleOrderLine> saleOrderLineList = saleOrderLineService.list(queryWrapper.eq("mid", id));
        return Result.ok(saleOrderLineList);
    }

    @AutoLog(value="销售订单-提交")
    @ApiOperation(value="销售订单-提交", notes="销售订单-通过id提交生成审批流")
    @PutMapping(value = "/submit")
    public Result<?> submit(@RequestParam(name="id",required=true) String id){
        System.out.println("开始提交");
        saleOrderService.submit("sale_order", id);

        return Result.ok("提交成功!");
    }

    @AutoLog(value="销售订单-审批")
    @ApiOperation(value="销售订单-审批", notes="销售订单-通过id审批")
    @PutMapping(value = "/approve")
    public Result<?> approve(@RequestBody JSONObject json){
        saleOrderService.approve("sale_order",json.getString("id"),json.getString("note"));
        return Result.ok("审批成功!");
    }

    @AutoLog(value="销售订单-拒绝")
    @ApiOperation(value="销售订单-拒绝", notes="销售订单-通过id拒绝")
    @PutMapping(value = "/reject")
    public Result<?> reject(@RequestBody JSONObject json){
        saleOrderService.reject("sale_order",json.getString("id"),json.getString("note"));
        return Result.ok("拒绝成功!");
    }

    @AutoLog(value="销售订单-撤回")
    @ApiOperation(value="销售订单-撤回", notes="销售订单-通过id撤回-只有已提交状态可以撤回")
    @PutMapping(value = "/withdraw")
    public Result<?> withdraw(@RequestParam(name="id",required=true) String id){
        saleOrderService.withDraw("sale_order",id);
        return Result.ok("撤回成功!");
    }

    @AutoLog(value="销售订单-作废")
    @ApiOperation(value="销售订单-作废", notes="销售订单-通过id作废")
    @PutMapping(value = "/invalid")
    public Result<?> withdraw(@RequestBody JSONObject json){
        saleOrderService.invalid(json.getString("id"), json.getString("note"));
        return Result.ok("作废成功!");
    }


    @AutoLog(value="销售订单-按钮操作权限")
    @ApiOperation(value="销售订单-按钮操作权限", notes="销售订单-按钮操作权限")
    @GetMapping(value = "/canApproveorReject")
    public Result<?> canApproveorReject(@RequestParam(name="id",required=true) String id){
        boolean canApproveorreject = saleOrderService.canApproveorReject("sale_order", id);
        JSONObject result = new JSONObject();
        result.put("canApproveorReject", canApproveorreject);
        return Result.ok(result);
    }

    @AutoLog(value="销售订单-查看审批记录")
    @ApiOperation(value="销售订单-查看审批记录", notes="销售订单-查看审批记录")
    @GetMapping(value = "/queryApproveRecords")
    public Result<?> queryApproveRecords(@RequestParam(name="id",required=true) String id){
        List<ApproveRecord> approveRecordList = saleOrderService.queryApproveRecordList("sale_order", id);
        return Result.ok(approveRecordList);
    }

}

